package com.gyg.bro.utils;

import org.apache.commons.lang3.StringUtils;

import java.util.Random;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 生成字符串工具类
 * @author: gyg
 */
public class RandomStringGenerator {

    /**
     * 生成length长度的随机数
     * @param length
     * @return
     */
    public static String getRandomString(int length) {
        //含有字符和数字的字符串
        String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        // 随机类初始化
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; ++i) {
            // [0,62)
            int number = random.nextInt(62);
            sb.append(str.charAt(number));
        }
        return sb.toString();
    }

    /**
     *
     * 随机生成验证码（数字+字母）
     *
     * @param len 邀请码长度
     * @return
     */
    public static String generateRandomStr(int len) {
        //字符源，可以根据需要删减
        //去掉1和i ，0和o
        String generateSource = "23456789abcdefghgklmnpqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        String rtnStr = "";
        for (int i = 0; i < len; i++) {
            //循环随机获得当次字符，并移走选出的字符
            String nowStr = String.valueOf(generateSource.charAt((int) Math.floor(Math.random() * generateSource.length())));
            rtnStr += nowStr;
            generateSource = generateSource.replaceAll(nowStr, "");
        }
        return rtnStr;
    }

    /**
     *
     * 随机生成验证码（数字）
     *
     * @param len 邀请码长度
     * @return
     */
    public static String generateRandomNum(int len) {
        //字符源，可以根据需要删减
        String generateSource = "1234567890";
        String rtnStr = "";
        for (int i = 0; i < len; i++) {
            //循环随机获得当次字符，并移走选出的字符
            String nowStr = String.valueOf(generateSource.charAt((int) Math.floor(Math.random() * generateSource.length())));
            rtnStr += nowStr;
            generateSource = generateSource.replaceAll(nowStr, "");
        }
        return rtnStr;
    }

    /**
     * 生成length长度的随机数
     * @param length
     * @return
     */
    public static String getRandomUserPersonalMainPageUrl(int length) {
        // 含有字符和数字的字符串
        String str = "abcdefghijklmnopqrstuvwxyz0123456789";
        // 随机类初始化
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; ++i) {
            // [0,36)
            int number = random.nextInt(36);
            sb.append(str.charAt(number));
        }
        return sb.toString();
    }

    /**
     * 生成36位uuid
     * @return
     */
    public static String get36UUIDstr() {
        return UUID.randomUUID().toString();
    }

    /**
     * 生成32位uuid
     * @return
     */
    public static String get32UUIDstr() {
        String uuid = get36UUIDstr();
        return uuid.substring(0, 8) + uuid.substring(9, 13)
                + uuid.substring(14, 18) + uuid.substring(19, 23)
                + uuid.substring(24);
    }

    /**
     * 获取业务号
     * @return
     */
    public static String getBizNo() {
        // 生成随机数
        Random rad = new Random();
        return System.currentTimeMillis() + "" + ((1000 + rad.nextInt(1000)) + "").substring(1);
    }


    /**
     * 验证输入 _- 数字英文
     * @param input
     * @return
     */
    public static boolean validateNewInput(String input){
        boolean sign=false;
        if(StringUtils.isBlank(input)){
            return sign;
        }
        String judge = "^[_\\-\\sa-zA-Z0-9]+$";
        Pattern pat = Pattern.compile(judge);
        Matcher mat = pat.matcher(input.trim());
        return mat.matches();
    }
}
